<?php
/**
 * CodeModel
 * 
 * @author Admin
 * @version 
 */
require_once 'Zend/Db/Table/Abstract.php';
class Wap_Models_FriendModel extends Zend_Db_Table_Abstract
{
    /**
     * The default table name 
     */
    protected $_name = 'friend';
    //function del friend
    function deluserbyfriendid($friendid,$userid){
       $db = Zend_Db_Table::getDefaultAdapter();
       $where = "(FriendID ='".$userid."' AND UserID ='".$friendid."') OR (FriendID ='".$friendid."' AND UserID ='".$userid."')";  
       //echo $where;
       $result = $db->delete('friend', $where);
       return $result;
    }
     //function get  get friend list by name
    function getnotfriendbyname($UserID,$name){
         //$arrParam['userID']='3EWVZLROJK';
    	  $db = Zend_Db_Table::getDefaultAdapter();
        $subsql = $db->select()->from('friend',array('FriendID'))->where("UserID!='".$UserID."'");
        $mySql = $db->select()->from(array('u'=>'userinfo'),array("u.UserID","u.Username","u.Avatar"))
                              ->where('UserID NOT IN ('.$subsql.')'." AND Username LIKE '%".$name."%'")
                              ->limit(10);
//                                                                                                                  ->limit(10);
//       echo $mySql;
//        die;
        $result = $db->fetchAll($mySql);
        return $result;
    }
    //function get  get friend list by name
    function getfriendbyname($UserID,$name){
         //$arrParam['userID']='3EWVZLROJK';
    	$db = Zend_Db_Table::getDefaultAdapter();
        $subsql = $db->select()->from('friend',array('FriendID'))->where("UserID='".$UserID."'");
        $mySql = $db->select()->from(array('u'=>'userinfo'),array("u.UserID","u.Username","u.Avatar"))
                              ->where('UserID IN ('.$subsql.')'." AND Username LIKE '%".$name."%'")
                              ->order("rand()")
                              ->limit(10);
      //  echo $mySql;
       // die;
        $result = $db->fetchAll($mySql);
        return $result;
    }
    //function get friend list ko phai ban
    function getrandomnotfriend($UserID,$pageid = null){
          //$arrParam['userID']='3EWVZLROJK';
    	$db = Zend_Db_Table::getDefaultAdapter();
        $subsql= $db->select()->from('friend',array("UserID"))->where("UserID ='".$UserID."' OR FriendID ='".$UserID."' ");
        //echo $subsql;
        $mySql = $db->select()->from(array('u'=>'userinfo'),array("u.UserID","u.Username","u.Avatar"))
                              ->where('UserID NOT IN ('.$subsql.')')
                              ->limitPage($pageid, 3);
//       
//        echo $mySql;
//        die;
        $result = $db->fetchAll($mySql);
        return $result;
    }
    function getFriendList($UserID,$pageid = null){
        //$arrParam['userID']='3EWVZLROJK';
    	$db = Zend_Db_Table::getDefaultAdapter();
    //                $subsql = $db->select()->from('friend',array('FriendID'))->where("UserID='".$UserID."'");
    //        $mySql = $db->select()->from('userinfo')
    //                              ->where('UserID  IN ('.$subsql.')')
    //                              ->order("UserID")
    //                               ->limitPage($pageid,10);
        $mySql = $db->select()->from(array('u'=>'userinfo'),array("u.UserID","u.Username","u.Avatar"))
                              
                ->join(array("f"=>"friend"), "u.UserID = f.FriendID",array())
                ->where("f.UserID='".$UserID."'")
                ->limitPage($pageid,10);
////      
////                              
//                               echo $mySql;
//                                  die;
        $result = $db->fetchAll($mySql);
   
//ZEND_DEBUG::dump($result);
     
        return $result;
    }
    function getnotFriendList($UserID,$pageid){
        //$arrParam['userID']='3EWVZLROJK';
    	$db = Zend_Db_Table::getDefaultAdapter();
        $subsql= $db->select()->from('friend',array("UserID"))->where("UserID ='".$UserID."' ");
        $subsql2  = $db->select()->from('friendrequest',array("ReceiverID"))->where("SenderID = '".$UserID."'");
        //echo $subsql;
        $mySql = $db->select()->from(array('u'=>'userinfo'),array("u.UserID","u.Username","u.Avatar"))
                              ->where('UserID NOT IN ('.$subsql.') AND UserID NOT IN ('.$subsql2.')')
                              ->limitPage($pageid,10);
//        echo $mySql;
//        die;
        $result = $db->fetchAll($mySql);
        return $result;
    }
    function checkFriend($user_id,$friend_id){
    	$db = Zend_Db_Table::getDefaultAdapter();
    	$mySql = $db->select()
    				->from(array('fr'	=> $this->_name),array("*"))
    				->where('fr.UserID = ?', $user_id)
    				->where('fr.FriendID = ?', $friend_id);
    	$result = $db->fetchRow($mySql);
    	return $result;
    }
    //function ghet lastest game play
    function getlastestgameplay($userid){
        $db = Zend_Db_Table::getDefaultAdapter();
        $subsql = $db->select()->from('userproduct',array('ProductID'))
                               ->where('UserID = ?',$userid)
                               ->order('LastTime DESC');
        $mySql = $db->select()->from('product',array('ProductID','DownloadUrl','GoogleDownloadUrl','avatar'))
                              ->where('ProductID IN ?',$subsql)
                              ->limit(5);
//        echo $mySql;
//        die;
        $result = $db->fetchAll($mySql);
//        Zend_Debug::dump($result);
//        die;
    	return $result;
                               
    }
    function addfriendrequest($data){
       $db = Zend_Db_Table::getDefaultAdapter();  
       $sqlcheck = $db->select()->from('friendrequest')->where("SenderID = '".$data['SenderID']."' AND ReceiverID ='".$data['ReceiverID']."'");
       $check = $db->fetchAll($sqlcheck);
       if (sizeof($check)>0){
           return FALSE;
       }else
       {
       $result = $db->insert('friendrequest',$data);
       if($result>0) return true;
       else return false;
       }
    }
    function listfriendrequest($userid,$pageid){
         $db = Zend_Db_Table::getDefaultAdapter();  
         $subsql = $db->select()->from('friendrequest',array("SenderID"))
                                ->where("ReceiverID= '".$userid."' AND ConfirmStatus = 0");
         $mySql = $db->select()->from('userinfo')->where(" UserID IN (".$subsql.")")->limitPage($pageid,10);;
//         $mySql = $db->select()->from(array('f'=>'userinfo'))
//                               ->join(array('fr'=>'friendrequest'),'fr.SenderID = userinfo.UserID')
//                               ->
//         echo $mySql;
//         die;
         $result= $db->fetchAll($mySql);
         return $result;
    }
    function getmutualfriend($userid,$friendid){
         $db = Zend_Db_Table::getDefaultAdapter(); 
         $subsql = $db->select()->from('friend',array("FriendID"))
                                ->where("UserID = '".$friendid."'");
         $mySql= $db->select()->from('friend',array("UserID"))
                              ->where("FriendID IN (".$subsql.")")
                              ->where("UserID =?",$userid);
//         echo $mySql;
//         die;
         $result= $db->fetchAll($mySql);
         return $result;
    }
    function addfrienddeny($userid,$friendid){
         $db = Zend_Db_Table::getDefaultAdapter(); 
         try{
//              $db->insert('friend', array('UserID' =>$userid,
//                                      'FriendID' =>$friendid));
//              $db->insert('friend', array('UserID' =>$friendid,
//                                     'FriendID' =>$userid));
              $db->delete('friendrequest', "ReceiverID = '".$userid."' AND SenderID='".$friendid."'");
              return true;
         }catch(Exception $e){
             echo $e;
             return false;
         }
    }
    function addfriendconfirm($userid,$friendid){
         $db = Zend_Db_Table::getDefaultAdapter(); 
         try{
              $db->insert('friend', array('UserID' =>$userid,
                                      'FriendID' =>$friendid));
              $db->insert('friend', array('UserID' =>$friendid,
                                     'FriendID' =>$userid));
           echo $db->update('friendrequest', array('ConfirmStatus'=>1), "ReceiverID = '".$userid."' AND SenderID='".$friendid."'");
              return true;
         }catch(Exception $e){
             echo $e;
             return false;
         }
    }
    function checkexistrequest($userid,$requestid){
         $db = Zend_Db_Table::getDefaultAdapter(); 
         $sql = $db->select()->from('friendrequest')->where("SenderID='".$requestid."' AND ReceiverID='".$userid."'" );
//         echo $sql;
//         die;
         $result= $db->fetchAll($sql);
//         echo sizeof($result);
//         die;
         if(sizeof($result)>0){
             return false;
         }else return true;
         
    }
    function loaduserinfobyid($uid){
        $db = Zend_Db_Table::getDefaultAdapter();  
        $sql=$db->select()->from('userinfo',array("UserID","Username","Avatar"))->where("UserID='".$uid."'");

        $result = $db->fetchAll($sql);
        return $result;
    }
    function loadfriendreq($senderid,$receiverid){
        $db = Zend_Db_Table::getDefaultAdapter();  
        $sql = $db->select()->from('friendrequest')->where("SenderID='".$senderid."' AND ReceiverID='".$receiverid."' AND ConfirmStatus = 0" );
        $result = $db->fetchRow($sql);
        return $result;
    }
    function update_notify($cond,$userid){
        $db = Zend_Db_Table::getDefaultAdapter();
        $where = "UserID ='".$userid."' AND Type =".$cond;
        $db->update('notification', array('Unread'=>0),$where);
    }
}       